package net.skystudio.es.controller;

import net.skystudio.es.common.AbstractController;
import net.skystudio.es.common.DBTypeEnum;
import net.skystudio.es.common.PageBean;
import net.skystudio.es.entity.ItemDesc;
import net.skystudio.es.service.ItemDescService;
import net.skystudio.es.vo.ItemDescVo;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/item")
public class ItemDescConntroller extends AbstractController {

    @Autowired
    private List<ItemDescService> itemDescServiceList;

    @PostMapping("/find")
    public ModelAndView find(ItemDescVo itemDescVo) {
        String key = itemDescVo.getKey();
        String dbTypeName = itemDescVo.getDbTypeName();
        long start = System.currentTimeMillis();
        ModelAndView mv = new ModelAndView("index");
        mv.addObject("resultList", getProxyService(dbTypeName).findBySearch(key));
        long end = System.currentTimeMillis();
        mv.addObject("elapsed", (end - start));
        mv.addObject("key", key);
        mv.addObject("dbTypeName", dbTypeName);
        mv.addObject("dbTypeList", new String[]{"MYSQL", "ES"});
        return mv;
    }

    @PostMapping("/paging")
    public ModelAndView paging(ItemDescVo itemDescVo) {
        long start = System.currentTimeMillis();
        String key = itemDescVo.getKey();
        String dbTypeName = itemDescVo.getDbTypeName();
        Integer page = Integer.valueOf(itemDescVo.getPage());
        Integer size = Integer.valueOf(itemDescVo.getSize());
        ModelAndView mv = new ModelAndView("index");
        PageBean<ItemDesc> itemDescPageBean = getProxyService(dbTypeName).pageQuery(page, size, key);
        long end = System.currentTimeMillis();
        mv.addObject("resultList", itemDescPageBean.getDatas());
        mv.addObject("elapsed", end - start);
        mv.addObject("key", key);
        mv.addObject("dbTypeName", dbTypeName);
        mv.addObject("dbTypeList", new String[]{"MYSQL", "ES"});
        mv.addObject("page", itemDescPageBean.getPageNum());
        mv.addObject("size", itemDescPageBean.getPageSize());
        mv.addObject("totalPage", itemDescPageBean.getTotalPage());
        mv.addObject("totalRow", itemDescPageBean.getTotalRow());
        mv.addObject("pageNum", itemDescPageBean.getPageNum());
        mv.addObject("hotWords", itemDescPageBean.getHotWords());
        return mv;
    }

    @GetMapping("/paging/{page}/{size}")
    public ModelAndView find(@PathVariable("page") Integer page,
                             @PathVariable("size") Integer size,
                             String dbTypeEnum,
                             String key) {
        ModelAndView mv = new ModelAndView("index");
        mv.addObject("resultList", getProxyService(dbTypeEnum).pageQuery(page, size, key));
        return mv;
    }

    private ItemDescService getProxyService(String dbType) {
        DBTypeEnum dbTypeEnum = DBTypeEnum.valueOf(dbType);
        Class<?> clazz = null;
        try {
            clazz = Class.forName(dbTypeEnum.getServiceName());
        } catch (ClassNotFoundException e) {
            throw new RuntimeException();
        }
        ItemDescService proxyService = null;
        for (ItemDescService itemDescService : itemDescServiceList) {
            if (AopUtils.getTargetClass(itemDescService) == clazz) {
                proxyService = itemDescService;
            }
        }
        return proxyService;
    }


}
